package br.com.eproducao.controller;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ManagerBeanBase {
	
	public void addInfo( String msg ){
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,msg, null)); 		
	}
	
	public boolean hasNoErrors(){
		return ! hasErrors();
	}
	
	public boolean hasErrors(){
		return ! FacesContext.getCurrentInstance().getMessageList().isEmpty();
	}
	
	public void addError( String msg ){
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,msg, null)); 		
	}

	public void addError( String msg, Throwable e ){
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,msg+": "+e.getMessage(), null)); 		
	}

	public void addAlert( String msg ){
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,msg, null)); 		
	}
	
	public HttpServletRequest getRequest(){
		return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
	}
	
	public HttpServletResponse getResponse(){
		return (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
	}

	public String getUrlProjeto(){
		String url = getRequest().getRequestURL().toString();
		return url.substring(0, url.lastIndexOf("/") );
	}
}
